<template>
  <div class="app-container">
    <!--搜索条件-->
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="活动名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入活动名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="审批状态" prop="approvalStatus">
        <el-select v-model="queryParams.approvalStatus" placeholder="请选择审批状态" clearable size="small">
          <el-option label="待审核" value="5"/>
          <el-option label="审核失败" value="10"/>
          <el-option label="审核成功" value="20"/>
        </el-select>
      </el-form-item>
      <el-form-item label="活动地点" prop="place">
        <el-select v-model="queryParams.place" placeholder="请选择活动地点" clearable size="small">
          <el-option label="校外" value="0"/>
          <el-option label="校内" value="1"/>
        </el-select>
      </el-form-item>
      <el-form-item label="活动时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <!--三个按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleApproval"
        >提交审批
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleBatchApproval"
        >批量提交审批
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!--主体表格-->
    <el-table border v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="活动标题" align="center" prop="title"/>
      <el-table-column label="开始时间" align="center" prop="begindate" width="180"/>
      <el-table-column label="结束时间" align="center" prop="enddate" width="180"/>
      <el-table-column label="活动地点" align="center" prop="place" :formatter="placeFormat"/>
      <el-table-column label="活动联系方式" align="center" prop="tel"/>
      <el-table-column label="活动规模" align="center" prop="scale"/>
      <el-table-column label="审批状态" align="center" prop="APPROVALSTATUS"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">

        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleApprovalDetail(scope.row)"
          >审批详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-order"
            @click="handleApproval(scope.row)"
          >提交审批
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 审批详情模态框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body hight="400px">

      <!--此处活动标题不规范，为了省事命名-->
      <el-table border v-loading="loading" :data="activityLogList">
        <el-table-column label="活动标题" align="center" prop="status"/>
        <el-table-column label="审批内容" align="center" prop="approvalName"/>
        <el-table-column label="审批结果" align="center" prop="log" width="180"/>
        <el-table-column label="审批时间" align="center" prop="createTime" width="180"/>
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listActivity,
    approvalActivity,
    approvalBatchActivity,
    approvalActivityLog,
    exportActivity
  } from '@/api/master/activity'

  export default {
    name: 'Activity',
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        //日期范围
        dateRange: [],
        // 总条数
        total: 0,
        // 活动主表格数据
        activityList: [],
        activityLogList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: undefined,
          begindate: undefined,
          enddate: undefined,
          place: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
    },
    methods: {
      placeFormat(row, column) {
        if (row.place === "1") {
          return '校内'
        } else  {
          return '校外'
        }
      },
      /** 查询活动主列表 */
      getList() {
        this.loading = true
        listActivity(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          // 主界面表格数据集定义为activityList，使用方法调用，完成数据集的定义
          this.activityList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          picture: undefined,
          title: undefined,
          begindate: undefined,
          enddate: undefined,
          place: undefined,
          tel: undefined,
          scale: undefined,
          status: '0'
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.dateRange = [];
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },

      /** 提交审批按钮操作 */
      handleApproval(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认提交审批该活动', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return approvalActivity(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('提交成功')
        })
      },

      //活动审批详情
      handleApprovalDetail(row) {
        this.reset()
        const id = row.id || this.ids
        approvalActivityLog(id).then(response => {
          this.activityLogList = response.data
          this.open = true
          this.title = '审批日志'
        })
      },

      /** 批量提交审批按钮操作 */
      handleBatchApproval(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认提交审批该活动' + ids, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return approvalBatchActivity(ids)
        }).then(() => {
          this.getList()
          this.msgSuccess('提交成功')
        })
      },

      //审批详情按钮
      approvalActivityLog() {
        const id = row.id || this.ids
        this.loading = true
        approvalActivityLog(id).then(response => {
          this.activityLogList = response.rows
          this.total = response.total
          this.loading = false
        })
      },

      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有活动主数据项?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return exportActivity(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
